//! 数据库模块核心功能测试
//! 
//! 测试 src/db/mod.rs 中的数据库填充器功能

use dataforge::db::SimpleDatabaseForge;
use dataforge::uuid_v4;
use dataforge::generators::name;
use dataforge::generators::internet;
use dataforge::generators::number;
use dataforge::generators::datetime;
use serde_json::json;

#[test]
fn test_simple_database_forge_creation() {
    let forge = SimpleDatabaseForge::new("sqlite://test.db");
    assert_eq!(forge.connection_string(), "sqlite://test.db");
    assert_eq!(forge.get_batch_size(), 1000);
}

#[test]
fn test_simple_database_forge_batch_size() {
    let forge = SimpleDatabaseForge::new("sqlite://test.db")
        .batch_size(2000);
    assert_eq!(forge.get_batch_size(), 2000);
}

#[test]
fn test_simple_database_forge_table_configuration() {
    let config = SimpleDatabaseForge::new("sqlite://test.db")
        .table("users", 100, |t| {
            t.field("id", || json!(uuid_v4()))
             .field("name", || json!(name::zh_cn_fullname()))
             .field("email", || json!(internet::email()))
        });
    
    assert_eq!(config.table_name(), "users");
    assert_eq!(config.count(), 100);
    assert_eq!(config.fields().len(), 3);
}

#[test]
fn test_table_fill_config_sync() {
    let config = SimpleDatabaseForge::new("sqlite://test.db")
        .table("test_table", 5, |t| {
            t.field("id", || json!(1))
             .field("name", || json!("test"))
        });
    
    let result = config.fill_sync();
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), 5);
}

#[cfg(feature = "database")]
#[tokio::test]
async fn test_table_fill_config_async() {
    let config = SimpleDatabaseForge::new("sqlite://test.db")
        .table("test_table", 5, |t| {
            t.field("id", || json!(1))
             .field("name", || json!("test"))
        });
    
    let result = config.fill().await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), 5);
}

#[test]
fn test_schema_parser_integration() {
    let forge = SimpleDatabaseForge::new("test://db");
    let parser = forge.schema_parser();
    
    // 测试Schema解析器是否正确初始化
    let json_schema = json!({
        "title": "Test",
        "properties": {
            "id": {"type": "integer"}
        }
    });
    
    let result = parser.infer_from_json(&json_schema);
    assert!(result.is_ok());
    
    let schema = result.unwrap();
    assert_eq!(schema.name, "Test");
    assert_eq!(schema.fields.len(), 1);
}

#[test]
fn test_multiple_field_types() {
    let config = SimpleDatabaseForge::new("sqlite://test.db")
        .table("complex_table", 10, |t| {
            t.field("id", || json!(uuid_v4()))
             .field("name", || json!(name::zh_cn_fullname()))
             .field("age", || json!(number::adult_age()))
             .field("email", || json!(internet::email()))
             .field("created_at", || json!(datetime::iso8601()))
             .field("active", || json!(true))
        });
    
    assert_eq!(config.fields().len(), 6);
    
    let result = config.fill_sync();
    assert!(result.is_ok());
}

#[test]
fn test_empty_table_configuration() {
    let config = SimpleDatabaseForge::new("sqlite://test.db")
        .table("empty_table", 0, |t| {
            // 不添加任何字段
            t
        });
    
    assert_eq!(config.count(), 0);
    assert_eq!(config.fields().len(), 0);
    
    let result = config.fill_sync();
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), 0);
}

#[test]
fn test_large_batch_configuration() {
    let config = SimpleDatabaseForge::new("sqlite://test.db")
        .batch_size(10000)
        .table("large_table", 50000, |t| {
            t.field("id", || json!(uuid_v4()))
             .field("data", || json!("test_data"))
        });
    
    assert_eq!(config.count(), 50000);
    // 注意：这里不实际执行填充，只测试配置
}

#[test]
fn test_different_connection_strings() {
    let connection_strings = vec![
        "sqlite://test.db",
        "mysql://user:pass@localhost/db",
        "postgresql://user:pass@localhost/db",
    ];
    
    for conn_str in connection_strings {
        let forge = SimpleDatabaseForge::new(conn_str);
        assert_eq!(forge.connection_string(), conn_str);
    }
}

#[test]
fn test_field_generator_execution() {
    let config = SimpleDatabaseForge::new("sqlite://test.db")
        .table("generator_test", 3, |t| {
            t.field("uuid", || json!(uuid_v4()))
             .field("name", || json!(name::zh_cn_fullname()))
             .field("number", || json!(number::random_int(1, 100)))
        });
    
    // 验证生成器能够正常执行
    let result = config.fill_sync();
    assert!(result.is_ok());
}

#[test]
fn test_schema_parser_access() {
    let mut forge = SimpleDatabaseForge::new("test://db");
    
    // 测试获取Schema解析器
    let parser = forge.schema_parser();
    assert!(parser.infer_from_json(&json!({"title": "Test"})).is_ok());
    
    // 测试获取可变Schema解析器
    let parser_mut = forge.schema_parser_mut();
    let result = parser_mut.add_pattern_rule(r"test_.*", 
        dataforge::db::DataType::String { max_length: Some(100) }, 50);
    assert!(result.is_ok());
}